<?php

namespace Modules\Admin\Models;

use DateTimeInterface;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class AuthAdmin extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = 'auth_admin';
    protected $guard = 'auth_admin';
    protected $hidden = [
        'password'
    ];
    public $fillable = [
        'name', 'username', 'password', 'avatar', 'status', 'nickname', 'phone', 'departmentid', 'email'
    ];

    /**
     * @description: jwt标识
     * @return {mixed}
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * @description: jwt自定义声明
     * @return {mixed}
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * @description: 更新时间为null时返回
     * @param {string} $value
     * @return {mixed}
     */
    public function getUpdatedAtAttribute($value)
    {
        return $value ? $value : '';
    }

    /**
     * @description: 关联权限组表   多对一
     * @return {mixed}
     */
    public function auth_groups()
    {
        return $this->belongsTo('Modules\Admin\Models\AuthGroup', 'group_id', 'id');
    }

    /**
     * @description: 关联平台项目表   多对一
     * @param {*}
     * @return {*}
     */
    public function auth_projects()
    {
        return $this->belongsTo('Modules\Admin\Models\AuthProject', 'project_id', 'id');
    }

    /**
     * @description: 时间格式转化
     * @param {DateTimeInterface} $date
     * @return {mixed}
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @return BelongsTo
     */
    public function department(): BelongsTo
    {
        return $this->belongsTo(AuthDepts::class);
    }

    /**
     * @return HasMany
     */
    public function adminRole(): HasMany
    {
        return $this->hasMany(AuthAdminRoles::class, 'user_id');
    }

    /**
     * @return HasManyThrough
     */
    public function role(): HasManyThrough
    {
        return $this->hasManyThrough(AuthRoles::class, AuthAdminRoles::class, 'user_id', 'id', 'id', 'role_id');
    }

    /**
     * @return void
     */
    public function attachRole(int $id, array $roleIds)
    {
        AuthAdminRoles::query()->where(['user_id' => $id])->delete();
        foreach ($roleIds as $roleId) {
            AuthAdminRoles::query()->create([
                'role_id' => $roleId,
                'user_id' => $id
            ]);
        }
        /* $this->adminRole()->delete();
        foreach ($roleIds as $roleId) {
            $this->adminRole()->create([
                'role_id' => $roleId
            ]);
        } */
    }

    public function delRoles(int $id)
    {
        AuthAdminRoles::query()->where(['user_id' => $id])->delete();
    }

    /**
     * @return string
     */
    public function getRoleString(): string
    {
        return $this->role()->get()->pluck('name')->implode(',');
    }

    /**
     * @return array
     */
    public function getRoleIds(): array
    {
        return $this->role()->get()->pluck('id')->toArray();
    }

    /**
     * @return array
     */
    public function getPerms(): array
    {
        $roles = $this->role()->get();
        $perms = [];
        foreach ($roles as $role) {
            $perms += $role->getMenuPerms();
        }
        return $perms;
    }

    /**
     * @return array
     */
    public function getMenus()
    {
        $roles = $this->role()->get();
        $menuIds = [];
        foreach ($roles as $role) {
            $menuIds += $role->getMenuIds();
        }
        $menuIds = array_unique($menuIds);

        return AuthMenu::query()->whereIn('id', $menuIds)->get();
    }
}
